package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelPrivilegeDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelPrivilegeMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelPrivilegeOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelPrivilegeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegeCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegePageRequest;
import com.cskaoyan.wordmemorize.service.LevelPrivilegeService;
import org.dom4j.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

@Service
public class LevelPrivilegeServiceImpl extends ServiceImpl<LevelPrivilegeMapper, LevelPrivilegeDO> implements LevelPrivilegeService {
    @Autowired
    LevelPrivilegeMapper levelPrivilegeMapper;

    @Autowired
    LevelConverter levelConverter;

    @Override
    public PageDTO<LevelPrivilegeDTO> getLevelPrivilegeList(LevelPrivilegePageRequest pageRequest) {
        if (pageRequest == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        Page<LevelPrivilegeDO> privilegeDOPage = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());

        String levelId = pageRequest.getLevelId();
        String keyword = pageRequest.getKeyword();
        LambdaQueryWrapper<LevelPrivilegeDO> wrapper = new LambdaQueryWrapper<LevelPrivilegeDO>()
                .eq(levelId != null, LevelPrivilegeDO::getLevelId, levelId)
                .like(keyword!=null, LevelPrivilegeDO::getPrivilegeCode, keyword)
                //这里是并的关系？
                .or()
                .like(keyword!=null, LevelPrivilegeDO::getPrivilegeName, keyword);
        Page<LevelPrivilegeDO> page = levelPrivilegeMapper.selectPage(privilegeDOPage, wrapper);

        long total = page.getTotal();
        List<LevelPrivilegeDO> records = page.getRecords();


        return levelConverter.levelPrivilegeDOToPageDTO(records, (int) total);
    }


    @Override
    public void addLevelPrivilege(LevelPrivilegeCommand command) {
        LevelPrivilegeDO levelPrivilegeDO = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);
        boolean save = save(levelPrivilegeDO);
        if (!save) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }


    }

    @Override
    public void updateLevelPrivilege(LevelPrivilegeCommand command) {
        LevelPrivilegeDO levelPrivilegeDO = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);
        boolean update = updateById(levelPrivilegeDO);
        if (!update) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
    }

    @Override
    public DeleteLevelPrivilegeOperateDTO deleteLevelPrivilege(Long id) {
        boolean remove = removeById(id);
        if (!remove) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }

        LevelPrivilegeDO levelPrivilegeDO = getById(id);
        return levelConverter.levelPrivilegeDOToDeleteLevelPrivilegeOperateDTO(levelPrivilegeDO);
    }

    @Override
    public List<DeleteLevelPrivilegeOperateDTO> batchDeleteLevelPrivilege(BatchDeleteCommand command) {
        if (command == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        levelPrivilegeMapper.deleteByIds(command.getIds());
        List<LevelPrivilegeDO> levelPrivilegeDOS = listByIds(command.getIds());
        return levelConverter.levelPrivilegeDOsToDeleteLevelPrivilegeOperateDTOs(levelPrivilegeDOS);

    }
}
